package com.example.administrator.netchess;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

public class RoomActivity extends Activity {

    private Button button_ready;
    private MyService.OptBinder optBinder = null;
    private boolean iIsWhite = false;


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case Common.FinalString.MESSAGE_ROOM_ACTIVITY:
                    break;

                case Common.FinalString.MESSAGE_START_GAME:
                    button_ready.setText("对战中");
                    iIsWhite = (boolean) msg.obj;
                    Wuziqi wuziqi= new Wuziqi(getBaseContext(), null);
                    wuziqi.setIsWhite(iIsWhite);
                    break;
            }

        }
    };

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            optBinder = (MyService.OptBinder) service;
            optBinder.setHandle(handler);

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);

        Intent intent = new Intent(RoomActivity.this,MyService.class);
        bindService(intent,conn,0);

        button_ready = (Button) findViewById(R.id.button_ready);
        button_ready.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (null!=optBinder){
                    JSONObject jo = new JSONObject();
                    try {
                        jo.put(Common.FinalString.CMD,Common.JsonCmdReq.READY);
                        jo.put(Common.FinalString.KEY_NAME,Common.FinalString.MY_NAME);
                        jo.put(Common.FinalString.IS_READY,true);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    optBinder.sendData(jo.toString());
                    button_ready.setText("等待对方准备");
                }
            }
        });





    }

    @Override
    protected void onResume() {
        super.onResume();
        //保证每次该Activity出现在屏幕最前端时，能通过handler收到消息
        if (null != optBinder) {
            optBinder.setHandle(handler);

        }
    }


    @Override
    protected void onDestroy() {

        if (null!=optBinder){
        JSONObject jo = new JSONObject();
        try {
            jo.put(Common.FinalString.CMD,Common.JsonCmdReq.APPLY_FOR_EXIT_ROOM);
            jo.put(Common.FinalString.KEY_NAME,Common.FinalString.MY_NAME);
            jo.put(Common.FinalString.KEY_ROOM_ID,Common.FinalString.MESSAGE_MAIN);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        optBinder.sendData(jo.toString());
        }


        if(null!=conn){
            unbindService(conn);
        }
        super.onDestroy();
    }



    //设置所需图片的大小，宽高
    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // 规定需要的图片的宽高
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }


    //压缩图片的方法，防止图片过大导致内存溢出
    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }



}
